package com.irdstudio.efp.esb.api.rest.zzd;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.irdstudio.efp.esb.api.bean.zzd.ReqUserRepaymentBean;
import com.irdstudio.efp.esb.api.bean.zzd.RespUserRepaymentBean;
import com.irdstudio.efp.esb.api.rest.EsbServerController;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.service.bo.req.RepaymentLoanReq;
import com.irdstudio.efp.esb.service.bo.resp.RepaymentLoanResp;
import com.irdstudio.efp.esb.service.facade.RepaymentLoanService;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.basic.framework.core.util.InterfaceCheckUtils;
import com.irdstudio.basic.framework.core.util.MessageRuleCheckUtil;
import com.irdstudio.basic.framework.core.util.StringUtil;

public class UserRepaymentController implements EsbServerController {

	private static Logger logger = LoggerFactory.getLogger(UserRepaymentController.class);
	@Autowired
	@Qualifier("repaymentLoanService")
	private RepaymentLoanService repaymentLoanService;

	/**
	 * 用户还款前置接口逻辑处理方法，处理逻辑包括：<br>
	 * 1.将用户借款请求报文体对象转换成用户借款请求对应的domain；<br>
	 * 2.对用户借款请求domain中值做是否必输性校验；<br>
	 * 3.对用户借款请求domain中值做是否合规性校验；<br>
	 * 4.调用对应的中台服务；<br>
	 * 
	 * @param esbReqBodyBean
	 * @return
	 * @throws Exception
	 * @author liyakui
	 * @since 2018年12月18日 下午5:16:37
	 * @version 1.0
	 */
	@Override
	public String execute(String esbReqBodyBean) throws ESBException, Exception {
		ReqUserRepaymentBean reqUserRepaymentBean = null;
		RespUserRepaymentBean esbRespBodyBean = new RespUserRepaymentBean();
		RepaymentLoanReq repaymentLoanReq = new RepaymentLoanReq();
		try {
			// 1.转换请求bean
			JSONObject reqJson = JSONObject.parseObject(esbReqBodyBean);
			reqUserRepaymentBean = JSON.toJavaObject(reqJson, ReqUserRepaymentBean.class);
			// 2.获取交易流水号或订单号
			logger.info("根据贷款订单号[" + reqUserRepaymentBean.getLoanListid() + "]进入用户还款前置交易处理开始");

			// 3.必输项校验
			String productCorpSpid = (String) InterfaceCheckUtils.getValue("productCorpSpid", "基金公司商户号",
					reqUserRepaymentBean.getProductCorpSpid(), true);
			String userIdentity = (String) InterfaceCheckUtils.getValue("userIdentity", "真身id", reqUserRepaymentBean.getUserIdentity(),
					true);
			String bankCusid = (String) InterfaceCheckUtils.getValue("bankCusid", "银行客户号", reqUserRepaymentBean.getBankCusid(), true);
			String loanListid = (String) InterfaceCheckUtils.getValue("loanListid", "贷款订单号", reqUserRepaymentBean.getLoanListid(), true);
			String loanAmount = (String) InterfaceCheckUtils.getValue("loanAmount", "贷款本金", reqUserRepaymentBean.getLoanAmount(), true);
			String curType = (String) InterfaceCheckUtils.getValue("curType", "币种", reqUserRepaymentBean.getCurType(), true);
			String repayListid = (String) InterfaceCheckUtils.getValue("repayListid", "还款订单号", reqUserRepaymentBean.getRepayListid(), true);
			String loanTime = (String) InterfaceCheckUtils.getValue("loanTime", "贷款时间", reqUserRepaymentBean.getLoanTime(), true);
			String repayTime = (String) InterfaceCheckUtils.getValue("repayTime", "还款时间", reqUserRepaymentBean.getRepayTime(), true);
			String repayAmount = (String) InterfaceCheckUtils.getValue("repayAmount", "还款金额", reqUserRepaymentBean.getRepayAmount(), true);
			String repayPrincipalAmount = (String) InterfaceCheckUtils.getValue("repayPrincipalAmount", "还款本金金额",
					reqUserRepaymentBean.getRepayPrincipalAmount(), true);
			String repayInterest = (String) InterfaceCheckUtils.getValue("repayInterest", "还款利息金额", reqUserRepaymentBean.getRepayInterest(),
					true);
			String stampDuty = (String) InterfaceCheckUtils.getValue("stampDuty", "印花税", reqUserRepaymentBean.getStampDuty(), true);
			String repayPenalty = (String) InterfaceCheckUtils.getValue("repayPenalty", "还款罚息", reqUserRepaymentBean.getRepayPenalty(),
					true);
			String explain = (String) InterfaceCheckUtils.getValue("explain", "还款说明", reqUserRepaymentBean.getExplain(), true);

			// 3.2.字段值合规性校验
			MessageRuleCheckUtil.checkDouble(reqJson.getString("repay_amount"), "还款金额");
			MessageRuleCheckUtil.checkDouble(reqJson.getString("repay_principal_amount"), "还款本金金额");
			MessageRuleCheckUtil.checkDouble(reqJson.getString("repay_interest"), "还款利息金额");
			MessageRuleCheckUtil.checkDouble(reqJson.getString("stamp_duty"), "印花税");
			MessageRuleCheckUtil.checkDouble(reqJson.getString("repay_penalty"), "还款罚息");

			// 3.3.调用中台服务：调用贷后还款式算服务，将返回还款金额(包含利息、罚息)，然后与第三方提供金额进行比对银行核对还款金额一致后，返回还款
			// 申请成功。如果申请金额不对，返回失败，避免用户部分还款。
			repaymentLoanReq.setAcctSeqNo("");
			repaymentLoanReq.setReceiptType("");
			repaymentLoanReq.setBaseAcctNo("");
			repaymentLoanReq.setReceiptAmt(new Double(""));
			repaymentLoanReq.setReceiptDate("");
			repaymentLoanReq.setReceiptType("");
			repaymentLoanReq.setReceiptPriAmt(new Double(""));
			RepaymentLoanResp paymentLoanResp = repaymentLoanService.repaymentLoanService(repaymentLoanReq);
			// 判断返回还款利息金额值是否一致
			if (!repayInterest.equals(paymentLoanResp.getPepayAmt())) {
				logger.info("还款利息金额不一致!");
				EsbBizEnums.RetCodeEnum.valueOf("1");
			}

			if (!repayPenalty.equals(paymentLoanResp.getTotalAmt())) {
				logger.info("还款罚息不一致！");
				EsbBizEnums.RetCodeEnum.valueOf("1");
			}
			// 3.4 封装数据
			esbRespBodyBean.setBankRetcode(EsbBizEnums.RetCodeEnum.SUCCESS.VALUE);
			esbRespBodyBean.setBankRetmsg(EsbBizEnums.RetCodeMsg.SUCCESS_MSG.VALUE);
			esbRespBodyBean.setRetcode(new Integer(EsbBizEnums.RetCodeMsg.SUCCESS_MSG.VALUE));
			esbRespBodyBean.setRetmsg(EsbBizEnums.RetCodeMsg.SUCCESS_MSG.VALUE);
			esbRespBodyBean.setRepayListid("");

			throw new ESBException();
		} catch (ESBException e) {
			e.printStackTrace();
			logger.error("");
		} catch (Exception e) {
			logger.error("");
		}
		logger.info("根据贷款订单号[" + reqUserRepaymentBean.getLoanListid() + "]进入用户还款前置交易处理结束");
		return JSON.toJSONString(esbRespBodyBean);
	}

	/**
	 * 必输项校验
	 * 
	 * @param reqBodyMap
	 * @throws BizException
	 * @author liyakui
	 * @since 2018年12月18日 下午8:33:03
	 * @version 1.0
	 */
	public void checkMessageRequired(Map<String, Object> reqBodyMap) throws BizException {
		Map<String, String> requiredMap = new HashMap<String, String>();
		requiredMap.put("sign_type", "签名方式");
		requiredMap.put("service_version", "接口版本");
		requiredMap.put("input_charset", "字符集");
		requiredMap.put("sign", "签名");
		requiredMap.put("loaner_spid", "银行商户号");
		requiredMap.put("file_name", "文件名");
		requiredMap.put("file_abstract", "授权文件摘要的值");
		requiredMap.put("product_corp_spi", "基金公司商户号");
		requiredMap.put("user_identity", "真身ID");
		requiredMap.put("bank_cusid", "银行客户号");
		requiredMap.put("loan_listid", "贷款订单号");
		requiredMap.put("loan_amount", "贷款本金");
		requiredMap.put("cur_type", "币种");
		requiredMap.put("repay_listid", "还款订单号");
		requiredMap.put("loan_time", "贷款时间");
		requiredMap.put("repay_time", "还款时间");
		requiredMap.put("repay_amount", "还款金额");
		requiredMap.put("repay_principal_amount", "还款本金金额");
		requiredMap.put("repay_interest", "还款利息金额");
		requiredMap.put("stamp_duty", "印花税");
		requiredMap.put("repay_penalty", "还款罚息");
		requiredMap.put("explain", "还款说明");

		StringUtil.checkRequiredValue(reqBodyMap, requiredMap);
	}

}
